﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucRightSlide : System.Web.UI.UserControl
    {
        private static SlideDO obj = new SlideDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            if (Request.QueryString["SlideID"] != null)
            {
                id = int.Parse(Request.QueryString["SlideID"].ToString());
            }
            obj = new SlideBL().Select(new SlideDO { SlideID = id });
            txtDescription.Text = obj.Description;
            txteDescription.Text = obj.eDescription;
            txtTitle.Text = obj.Title;
            txteTitle.Text = obj.eTitle;
            txtLinkAnh.Text = obj.SlideURL;
            trPopup.Visible = true;
            hplImage.ImageUrl = "~/Media/" + obj.SlideImg;
        }
        private string UploadFile(FileUpload fulImage)
        {
            if (fuImage.HasFile)
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }
        private SlideDO init()
        {
            obj.Description = txtDescription.Text;
            obj.eDescription = txteDescription.Text;
            obj.Title = txtTitle.Text;
            obj.eTitle = txteTitle.Text;
            obj.SlideURL = txtLinkAnh.Text;
            if (UploadFile(fuImage)!="" )
            {
                obj.SlideImg = UploadFile(fuImage);
            }
            return obj;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            obj = init();
            new SlideBL().Update(obj);
            Functions.Alert("Cập nhật thành công!", "Default.aspx?Page=ListAlbumRightSlide");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
        }
    }
}